﻿/**
* author jwf 2016-7-8
* 用户相关的类
执行成功，则返回
{
    status:'0',
    其他字段
}
执行出错直接返回错误码

*/
angular.module('com.xbstar.user', [])
.factory('User',['$q','JFHttp','APPTools',function ($q,JFHttp,APPTools) {
    //var baseurl="http://106.14.30.199:8080";
    var baseurl="http://106.14.30.199:8080";
    var loginurl=baseurl+"/user/login";
    var registurl=baseurl+"/user/regist";
    var isregistedurl=baseurl+"/zhyl-relation/user/isRegister";
    var getinfourl=baseurl+"/zhyl-relation/user/selectUserInfo";
    var uploadPicurl=baseurl+"/zhyl-relation/user/updateUserInfo";
    // var updatePWDurl=baseurl+"/zhyl-relation/user/updateUserPwd";
     var updatePWDurl="http://222.82.250.210:8091"+"/zhyl-relation/user/updateUserPwd";
    /**
    * 判断是否登录，name为base64加密或者为空
    */
    var islogined=function(name){
        var key="loginkey";
        var uname=localStorage.getItem(key);
        if (!name) {
            if (uname) return true;
        }else{
            if (name===uname) return true;
        }
        return false;
    }
    /**
    * 获得用户登录key
    */
    var getUserKey=function () {
         var key="loginkey";
         var uname=localStorage.getItem(key);
         if (uname) return APPTools.decode(uname);
         return undefined;
    }
    /**
    * 用户登录
    */
    var login=function (name,pwd) {
        if(!name||!pwd) throw new Error("User-name or pwd is null");
        /**
         * 对pwd采用md5加密，对name采用base64加密
         */
       var tpwd=APPTools.md5(pwd);
       var tname=APPTools.encode(name+"");
       var deferred=$q.defer();
        //查看本地是否登录此账号
        if (islogined(tname)) deferred.resolve({status:0});
        else{
            JFHttp.post({
                url:loginurl,
                data:{
                    userId:tname,
                    userPassword:tpwd
                }
            }).then(function(data){
                if (data&&data.status!=undefined) {
                    if (data.status=="200") {
                        //登录成功
                        var ticket1={};
                        ticket1.loginkey=data.loginkey;
                        ticket1.time=data.time;
                        ticket1.token=data.token;
                        localStorage.setItem("ticket",JSON.stringify(ticket1));
                        localStorage.setItem("loginkey",tname);
                        deferred.resolve({status:0});
                    }else{
                         deferred.reject(data.status);
                    }
                }
            },function(data){
                deferred.reject(data.status);
            });
        }
        return  deferred.promise;
    }
    /**
    * 用户更改用户头像
    */
    ;var uploadPic=function (params) {
       var ticket1=ticket();
       var deferred=$q.defer();
       //alert("update user pic ");
       JFHttp.post({
                url: uploadPicurl,
                data:{
                    affixId:params.affixId,
                    ticket:ticket1
                }
            }).then(function(data){
               // alert(data.status);
                if (data&&(data.status=="0"||data.status==0)) {
                    if (data.status==0) {
                        deferred.resolve({status:0});
                    }else{
                         deferred.reject(data.status);
                    }
                }
            },function(data){
                //alert(data.status);
                deferred.reject(data.status);
            });
        return  deferred.promise;
    }
    /**
    * 用户进行注册
    */
    var regist=function(name,pwd){
        if(!name||!pwd) throw new Error("User-name or pwd is null");
        /**
         * 对pwd采用md5加密，对name采用base64加密
         */
       var tpwd=APPTools.md5(pwd);
       var tname=APPTools.encode(name);
       var deferred=$q.defer();
       JFHttp.post({
                url:registurl,
                data:{
                    name:tname,
                    pwd:tpwd
                }
            }).then(function(data){
                if (data&&data.status==0) {
                    if (data.status==0) {
                        deferred.resolve({status:0});
                    }else{
                         deferred.reject(data.status);
                    }
                }
            },function(data){
                deferred.reject(data.status);
            });
        return  deferred.promise;
    }
    /**
    * 判断用户是否注册
    */
    ;var isregisted=function(name){
        if(!name) throw new Error("User-name or pwd is null");
        var tname=APPTools.encode(name);
        var deferred=$q.defer();
        JFHttp.post({
                url:isregistedurl,
                data:{
                    userInfoId:tname
                }
            }).then(function(data){
                if (data&&data.status) {
                    if (data.status==0) {
                        deferred.resolve({status:0});
                    }else{
                         deferred.reject(data.status);
                    }
                }
            },function(data){
                deferred.reject(data.status);
            });
         return  deferred.promise;    
    }
    /**
    * 获取用户票据
    */
    ;var ticket=function () {
        var ticket1=localStorage.getItem("ticket");
        var name=localStorage.getItem("loginkey");
        if (name) {
            if (ticket1) return ticket1;
            return "";
        }
        return "";
    }
    /**
    * 用户基本信息
    */
    ;var getUserInfo=function (flag) {
        var ticket1=ticket();
        if (!ticket) console.error("ticket is null,but is logined");
        var deferred=$q.defer();
        var tuser=localStorage.getItem("userrtrry");
        if (tuser&&!flag) {
            deferred.resolve(angular.fromJson(tuser));
            return  deferred.promise;    
        }
        JFHttp.post({
            data:{ticket:ticket1},
            url:getinfourl,
        }).then(function(data) {
            //alert(JSON.stringify(data));
            var tuss={
                   telephoneNumber:data.userInfoId,
                   affixPath:data.belongTo 
                };
            localStorage.setItem("userrtrry",JSON.stringify(tuss));
            deferred.resolve(tuss);
        },function(data) {
            deferred.reject(data.status);
        });
         return  deferred.promise;    
    }
    /**
    * 退出当前用户登录
    */
  ;var loginout=function() {
      var flag=true;
     try {
          localStorage.clear();
     } catch (error) {
         flag=false;
     }
     return flag;
  }
  /**
  * 修改密码
  */
  ;var updatePWD=function(newpwd){
      if (!newpwd) {
          return;
      }
       var deferred=$q.defer();
        JFHttp.post({
            data:{
                userInfoId:getUserKey(),
                userPassword:APPTools.md5(newpwd)
            },
            url:updatePWDurl,
        }).then(function(data) {
           if (data) { 
               deferred.resolve(data);
           }
        },function(data) {
            deferred.reject(data.status);
        });
         return  deferred.promise;    
  }
  ;return{
      /**
      * 用户登录
      */
        login:function (name,pwd) {
           return login(name,pwd); 
        }
        /**
        * 判断用户是否登录
        */
        ,islogined:function(name){
            return islogined(name);
        }
        /**
        * 用户注册
        */
        ,regist:function(name,pwd){
            return regist(name,pwd);
        }
        ,isregisted:function (name) {
            return isregisted(name);
        }
        /**
        * 获取用户票据
        */
        ,ticket:function () {
            return ticket();
        }
        /**
        * 获得用户基本信息
        */
        ,getUserInfo:function (flag) {
            return getUserInfo(flag);
        }
        ,getUserKey:function () {
            return getUserKey();
        }
        ,loginout:function () {
            return loginout();
        }
        ,uploadPic:function (params) {
            return uploadPic(params);
        }
        ,updatePWD:function(newpwd){
            return updatePWD(newpwd);
        }
  }
}])